/*
 * Product Controller
 * @Author: chiangbt@geoportal.cn 
 * @Date: 2019-05-07 13:52:17 
 * @Last Modified by: chiangbt@geoportal.cn
 * @Last Modified time: 2019-05-09 11:06:45
 */
const mongoose = require('mongoose')
const Product = require('../models/product');

exports.products_get_all = (req, res, next) => {
    const limit = parseInt(req.query.limit) || 15
    const offset = parseInt(req.query.offset) || 0

    Product.count({}, function (err, counts) {
        Product.find().skip(offset).limit(limit)
            .sort({
                '_id': -1
            })
            .select('_id name price createdAt') // 限制返回字段
            .then(products => {
                // 返回prodcuts的同时对返回结果进行构造
                return res.status(200).json({
                    success: true,
                    count: counts,
                    products: products.map(product => {
                        return {
                            id: product._id,
                            name: product.name,
                            price: product.price,
                            createdAt: product.createdAt
                        }
                    })
                });
            })
            .catch(err => {
                return res.status(500).json({
                    success: false,
                    error: err.message
                });
            });
    });
};

exports.create_product = (req, res, next) => {
    const product = new Product({
        _id: new mongoose.Types.ObjectId(),
        name: req.body.name,
        price: req.body.price
    });

    product.save().then(result => {
        return res.status(201).send({
            success: true,
            message: '创建 products 对象成功.',
            product: {
                name: result.name,
                price: result.price,
                id: result._id,
                createdAt: result.createdAt
            }
        })
    }).catch(err => {
        return res.status(500).json({
            success: false,
            error: err.message
        });
    });
};

exports.get_product_by_id = (req, res, next) => {
    const id = req.params.id;

    Product.findById(id)
        .select('_id name price createdAt')
        .then(product => {
            if (product) {
                return res.status(200).json({
                    success: true,
                    result: product
                });
            } else {
                return res.status(404).json({
                    success: false,
                    message: "No valid entry found for provided ID"
                });
            }
        })
        .catch(err => {
            return res.status(500).json({
                success: false,
                error: err.message
            });
        });
};

exports.update_product = (req, res, next) => {
    const id = req.params.id;
    const product = req.body;
    const {
        ...updateData
    } = product

    Product.findByIdAndUpdate({
            _id: id
        }, updateData, {
            new: true
        })
        .select('_id name price')
        .then(result => {
            if (result) {
                return res.status(200).json({
                    success: true,
                    result: result
                });
            } else {
                return res.status(400).json({
                    success: false,
                    error: '更新的product并不存在'
                });
            }
        })
        .catch(err => {
            return res.status(500).json({
                success: false,
                error: err.message
            });
        });
};



exports.delete_product = (req, res, next) => {
    Product.findOneAndRemove({
            _id: req.params.id
        })
        .then(result => {
            if (result) {
                return res.status(200).json({
                    success: true,
                    message: '指定product对象删除成功'
                });
            } else {
                return res.status(400).json({
                    success: false,
                    error: '指定要删除的product对象不存在'
                });
            }
        })
        .catch(err => {
            return res.status(400).json({
                success: false,
                error: err.message
            });
        });
};